<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="list" style="display: flex; text-align:center"></div>
    <button class="previous">上一页</button>
    <button class="next">下一页</button>
    <span></span>
    
</body>

</html>
<script>
    function ajax(fangshi, url, cb) {
        //1.创建一个请求对象
        let xhr = new XMLHttpRequest()

        //2.配置请求
        xhr.open(fangshi, url)

        //3.设置回调函数
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                cb(JSON.parse(xhr.responseText))

            }
        }

        //4.发送请求
        xhr.send()

    }
    var page=1
   var  previous=document.querySelector(".previous")
   var  next=document.querySelector(".next")
   if(page==1){
    previous.disabled=true
   }
   ajax("get", `https://yantianfeng.com/api/emotion?page=${1}&count=${10}`, function (data) {

            let str = data.emotions.map((item, index) => {
                return `<div> <img src="${item.url}"/> <div>${item.title}</div> </div>`
            }).join("")
            document.querySelector(".list").innerHTML = str
            document.querySelector("span").innerHTML = `${page}/4`
        })


      previous.onclick=function(){
      if(page>1){
       
        ajax("get", `https://yantianfeng.com/api/emotion?page=${--page}&count=${10}`, function (data) {
            let str = data.emotions.map((item, index) => {
                return `<div> <img src="${item.url}"/> <div>${item.title}</div> </div>`
            }).join("")
            document.querySelector(".list").innerHTML = str
            document.querySelector("span").innerHTML = `${page}/4`
        })

      }
     
    }
    next.onclick=function(){
       if(page<4){
        previous.disabled=false
     
        ajax("get", `https://yantianfeng.com/api/emotion?page=${++page}&count=${10}`, function (data) {
            let str = data.emotions.map((item, index) => {
                return `<div> <img src="${item.url}"/> <div>${item.title}</div> </div>`
            }).join("")
            document.querySelector(".list").innerHTML = str
            document.querySelector("span").innerHTML = `${page}/4`
        })
       }
    }
</script>